﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TinyTeam.UI;
using System;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Util : MonoBehaviour
{
    public static Util instance;

    private void Awake()
    {
        instance = this;
        DontDestroyOnLoad(gameObject);
    }

    void Start()
    {
        
    }

    //一段时间后执行逻辑
    public Coroutine WaitForSec(float sec, Action cb)
    {
        return StartCoroutine(WaitForSecCor(sec, cb));
    }

    IEnumerator WaitForSecCor(float sec, Action cb)
    {
        yield return new WaitForSeconds(sec);
        cb();
    }

    //加载某个资源后执行逻辑
    public Coroutine WWWGet(string url, Action<WWW> cb)
    {
        return StartCoroutine(WWWGetCor(url, cb));
    }

    IEnumerator WWWGetCor(string url, Action<WWW> cb)
    {
        WWW www = new WWW(url);
        yield return www;
        if (www.error == null)
        {
            cb(www);
        }
    }

    //加载某个资源后执行逻辑
    public Coroutine WWWPost(string url, WWWForm form, Action<WWW> cb)
    {
        return StartCoroutine(WWWPostCor(url, form, cb));
    }

    IEnumerator WWWPostCor(string url, WWWForm form, Action<WWW> cb)
    {
        WWW www = new WWW(url, form);
        yield return www;
        if (www.error == null)
        {
            cb(www);
        }
    }

    //播放声音
    public void PlaySound(AudioClip clip)
    {
        GameObject go = new GameObject("Audio");
        AudioSource asr = go.AddComponent<AudioSource>();
        asr.clip = clip;
        asr.Play();
        Destroy(go, clip.length + 1);
    }

    //打字机效果
    public Coroutine Typewriter(string content, float dertTime, Text text, Action cb)
    {
        return StartCoroutine(TypewriterCor(content, dertTime, text, cb));
    }

    IEnumerator TypewriterCor(string content, float dertTime, Text text, Action cb)
    {
        if (text != null)
        {
            string temp = "";
            for (int i = 0; i < content.Length; i++)
            {
                temp += content[i];
                text.text = temp;
                yield return new WaitForSeconds(dertTime);
            }
        }

        if (cb != null)
        {
            cb();
        }
    }

    //加载某个场景
    public Coroutine LoadingScene(string sceneName, Image fg, Image point, Text pro, UnityAction<Scene,LoadSceneMode> cb)
    {
        return StartCoroutine(LoadingSceneEnu(sceneName, fg, point, pro, cb));
    }

    IEnumerator LoadingSceneEnu(string sceneName, Image fg, Image point, Text pro, UnityAction<Scene,LoadSceneMode> cb)
    {
        SceneManager.sceneLoaded += cb;
        AsyncOperation oper = SceneManager.LoadSceneAsync(sceneName,LoadSceneMode.Single);
        oper.allowSceneActivation = false;
        float cur = 0;
        float real = 0;
        TipUI.isShowing = false;
        while (true)
        {
            if (oper.progress * 100 >= real)
            {
                real = oper.progress * 100;
                if (real >= 90) real = 100;
            }

            if (cur < real)
            {
                cur++;
            }
            else if (cur >= 100)
            {
                break;
            }

            if (fg != null && point != null && pro != null)
            {
                fg.fillAmount = cur / 100;
                point.transform.localPosition = fg.transform.localPosition - Vector3.right *
                                                (fg.rectTransform.sizeDelta.x / 2 -
                                                 fg.fillAmount * fg.rectTransform.sizeDelta.x);
                pro.text = Math.Round(cur) + "%";
            }

            yield return null;
        }

        oper.allowSceneActivation = true;
    }

    //>>>>>>>>>>>
    public string GetVideoUrl(string videoName)
    {
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
        return "file://assets/movie/" + videoName;

#else
        return "./video/" + videoName;
#endif
    }

    public string GetTimeStrBySec(double sec)
    {
        int t = (int) Math.Round(sec);
        int h = t / 3600;
        int m = t / 60;
        int s = t % 60;
        return GetTimeStrByNum(h) + ":" + GetTimeStrByNum(m) + ":" + GetTimeStrByNum(s);
    }

    private string GetTimeStrByNum(int num)
    {
        if (num < 10)
        {
            return "0" + num;
        }

        return num.ToString();
    }

    public void ChangeMaterial(GameObject obj, string matName)
    {
        Material material = Resources.Load<Material>(matName) as Material;
        obj.GetComponent<MeshRenderer>().materials[0].CopyPropertiesFromMaterial(material); //更改材质球
    }

    IEnumerator ACRet(Action cb)
    {
        while (true)
        {
            if (TTUIPage.allPages.ContainsKey("SYXXUI") && TTUIPage.allPages["SYXXUI"] != null
                                                        && TTUIPage.allPages["SYXXUI"].gameObject != null)
            {
                cb();
                break;
            }

            yield return null;
        }
    }

    public void ASRec(Action cb)
    {
        StartCoroutine(ACRet(cb));
    }

    IEnumerator ACRct(Action cb)
    {
        while (true)
        {
            if (TTUIPage.allPages.ContainsKey("QiYeBeiAnUI") && TTUIPage.allPages["QiYeBeiAnUI"] != null
                                                             && TTUIPage.allPages["QiYeBeiAnUI"].gameObject != null)
            {
                cb();
                break;
            }

            yield return null;
        }
    }

    public void ASRvc(Action cb)
    {
        StartCoroutine(ACRct(cb));
    }

    public bool IsOnGUI
    {
        get
        {
            PointerEventData eventData = new PointerEventData(EventSystem.current);
            eventData.pressPosition = Input.mousePosition;
            eventData.position = Input.mousePosition;

            List<RaycastResult> list = new List<RaycastResult>();
            TTUIRoot.Instance.graphicRaycaster.Raycast(eventData, list);
            return list.Count > 0;
        }
    }

}